// pages/home/home.js
// 主页
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollId: 'performer',
    currentTopNavIndex: 0,
    homeTopNavList: [{
        id: 1,
        value: '网红',
        scrollName: 'performer'
      },
      {
        id: 2,
        value: '摄影师',
        scrollName: 'photographer'
      },
      {
        id: 3,
        value: '化妆师',
        scrollName: 'dresser'
      },
      {
        id: 4,
        value: '场地',
        scrollName: 'field'
      },
    ],
    searchInputIcon: '/assets/icons/lookup.png',
    moreIcon: '/assets/icons/more-d.png',
    addressIcon: '/assets/icons/address.png',
    performerDataList: [],
    photographerDataList: [],
    dresserDataList: [],
    fieldDataList: [],
  },
  clickSearch: function() {
    // 点击搜索输入框，跳转到搜索页面
    wx.navigateTo({
      url: '/pages/homeSearch/homeSearch',
    })
  },
  clickTopNav: function(e) {
    // 选中顶部导航
    let index = e.currentTarget.dataset.index
    let scrollName = e.currentTarget.dataset.scrollname
    this.setData({
      currentTopNavIndex: index,
      scrollId: scrollName
    })
  },

  openMore: function(e) {
    // 查看更多
    let navId = e.currentTarget.dataset.navid
    wx.setStorage({
      key: 'navid',
      data: navId,
    })
    wx.navigateTo({
      url: '/pages/list/list?navId=' + navId,
    })
  },
  clickHomePage: function(e) {
    // 点击打开个人主页
    let id = e.currentTarget.dataset.id
    let navId = e.currentTarget.dataset.navid
    wx.setStorage({
      key: 'navid',
      data: navId,
    })
    wx.navigateTo({
      url: '/pages/homePage/homePage?id=' + id,
    })
  },
  scrollChange: function(e) {
    // 滚动页面改变导航高亮
    let page = this
    let scrollTop = e.detail.scrollTop + 189
    if (scrollTop >= page.data.performerBoxTop && scrollTop < page.data.photographerBoxTop) {
      page.setData({
        currentTopNavIndex: 0
      })
    } else if (scrollTop < page.data.dresserBoxTop) {
      page.setData({
        currentTopNavIndex: 1
      })
    } else if (scrollTop < page.data.fieldBoxTop) {
      page.setData({
        currentTopNavIndex: 2
      })
    } else {
      page.setData({
        currentTopNavIndex: 3
      })
    }
  },
  labelScreen: function(labelList) {
    // 首页筛选标签数量
    if (labelList.length > 2) {
      if (labelList[0].length > 2 || labelList[1].length > 2) {
        return labelList.slice(0, 2)
      }
    }
    return labelList.slice(0, 3)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this
    let config = wx.getStorageSync('config')
    let homeDataUrl = config.host + 'api/product/queryIndex'
    // 加载首页数据
    wx.request({
      url: homeDataUrl,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.msg == 'ok' && res.data.status == 200) {
          let performerData = []
          let photographerData = []
          let dresserData = []
          let fieldData = []
          res.data.data.celebrityList.forEach(item => {
            item.label = item.label.split(',')
            // 筛选标签
            item.label = _this.labelScreen(item.label)
            performerData.push(item)
          })
          res.data.data.makeupList.forEach(item => {
            item.label = item.label.split(',')
            item.label = _this.labelScreen(item.label)
            dresserData.push(item)
          })
          res.data.data.photographerList.forEach(item => {
            item.label = item.label.split(',')
            item.label = _this.labelScreen(item.label)
            photographerData.push(item)
          })
          res.data.data.siteList.forEach(item => {
            item.label = item.label.split(',')
            item.label = _this.labelScreen(item.label)
            fieldData.push(item)
          })


          _this.setData({
            performerDataList: performerData,
            dresserDataList: dresserData,
            photographerDataList: photographerData,
            fieldDataList: fieldData
          })
        }
      }
    })

    //滚动页面
    this._observer = wx.createIntersectionObserver(this)
    this._observer.relativeTo('.scrollContent').observe('.course-info', res => {
      this.setData({
        appear: res.intersectionRatio > 189
      })
    })
    setTimeout(() => {
      new Promise(resolve => {
        let query = wx.createSelectorQuery();
        query.select('#performer').boundingClientRect();
        query.select('#photographer').boundingClientRect();
        query.select('#dresser').boundingClientRect();
        query.select('#field').boundingClientRect();
        query.exec(function(res) {
          resolve(res);
        });
      }).then(res => {
        _this.setData({
          performerBoxTop: res[0].top,
          photographerBoxTop: res[1].top,
          dresserBoxTop: res[2].top,
          fieldBoxTop: res[3].top,
          loaded: 1
        });
        // Toast.clear();
      });
    }, 600)
  },
  onShow: function() {
    wx.showTabBar()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  }
})